package com.example.gzysmartcity.ui.fragment.traffic;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentBuslistBinding;
import com.example.gzysmartcity.ui.adapter.BusListAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.BusListViewModel;

import org.jetbrains.annotations.NotNull;

public class BusListFragment extends BaseFragment<FragmentBuslistBinding> {
    @Override
    protected FragmentBuslistBinding viewBinding() {
        return FragmentBuslistBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        BusListViewModel buslistViewModel = new ViewModelProvider(this).get(BusListViewModel.class);
        b.rvBuslist.setLayoutManager(new LinearLayoutManager(getContext()));
        buslistViewModel.getBusData();
        buslistViewModel.getBusLivedata().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    Log.i("TAG", "onViewCreated: ");
                    BusListAdapter adapter = new BusListAdapter(R.layout.rv_item_buslist);
                    adapter.setNewInstance(buslistViewModel.getMyCityBus());
                    b.rvBuslist.setAdapter(adapter);
                    adapter.setOnItemClickListener((adapter1, view1, position) -> {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("buslist", buslistViewModel.getMyCityBus().get(position));
                        jump(BusContentFragment.class, bundle, "buscontent");
                    });
                    break;
                case FAIL:
                    ToastUtils.showShort("加载失败");
                    break;
            }
        });
        b.back.setOnClickListener(v -> remove());
        b.tvPark.setOnClickListener(v -> jump(ParkFragment.class, null, null));
    }

    @Override
    protected String setTitle() {
        return null;
    }

    @Override
    protected boolean showBack() {
        return false;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
